<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="value" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container style="overflow:hidden;">
			<el-header>
				<div class="left-panel">
					<span style="font-weight: 600;font-size: 18px">{{ (photoName != '' ? '《' + photoName + '》' : '') }}</span>
				</div>
				<div class="right-panel">
					<sc-upload-file :apiObj="apiObj" v-model="fileList" :limit="10" :data="{photoId:currentPhotoId}" :showFileList="false" v-if="currentPhotoId!=''&&manageRole"
									@success="uploadSuccess" accept="image/*">
						<el-button type="primary" icon="el-icon-upload">上传图片</el-button>
					</sc-upload-file>
				</div>
			</el-header>
			<el-container>
				<el-card shadow="never" style="width:100%;height: 100%;overflow: auto;">
					<el-alert v-if="this.currentPhotoId==''" title="请选择相册查看相片" type="warning" style="margin-bottom:20px;"></el-alert>
					<el-col v-if="photoFileList.length==0">
						<el-empty description="目录下没有相片" :image-size="100"></el-empty>
					</el-col>
					<el-row :gutter="15">
						<el-col style="margin-bottom: 10px;" :xl="3" :lg="3" :md="4" :sm="6" :xs="24" v-for="item in photoFileList" :key="item.fileName"
								@contextmenu.prevent="openMenu($event,item)">
							<div style="text-align: center;height: 250px;display: contents;">
								<el-image class="image" :src="item.url" :preview-src-list="previewList" @click="previewImage(item.url)" style="height: 250px;width: 100%"></el-image>
							</div>
							<div style="height: 70px">
								<h3 style="text-align: center;overflow:hidden;white-space:nowrap;"> {{item.fileName}} </h3>
								<div class="nameClass">
									大小:{{item.fileSize}}
								</div>
								<div class="nameClass">
									时间:{{ item.createTime }}
								</div>
							</div>
						</el-col>
					</el-row>
				</el-card>
			</el-container>
		</el-container>
	</el-container>
	<sc-contextmenu ref="fileContextmenu" @command="handleCommand">
		<sc-contextmenu-item v-if="manageRole" command="delete" title="删除" icon="el-icon-delete"></sc-contextmenu-item>
		<sc-contextmenu-item command="down" title="下载" icon="el-icon-download"></sc-contextmenu-item>
	</sc-contextmenu>
</template>
<script>
import scContextmenu from '@/components/scContextmenu'
import scContextmenuItem from '@/components/scContextmenu/item'
import sysConfig from "@/config";
export default {
	name: 'My Photo List',
	components: {
		scContextmenu,
		scContextmenuItem,
	},
	data() {
		return {
			manageRole:false,
			showGrouploading: false,
			groupFilterText: '',
			photoFileList: [],
			photoName: '',
			currentPhotoId: '',
			currentPhotoFile:'',
			previewList:[],
			fileList:[],
			group: [],
			groupsProps: {
				label: "label",
				value: "value"
			},
			apiObj:this.$API.file.photo.createPhotoFile,
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getMyPhotoList()
	},
	methods: {
		//加载树数据
		async getMyPhotoList() {
			this.showGrouploading = true;
			var res = await this.$API.file.photo.getMyPhotoList.get();
			this.showGrouploading = false;
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.label.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			this.currentPhotoId = data.value;
			this.photoName = data.label;
			this.getMyPhotoFileList();
			this.getPhotoRole();
		},
		async getMyPhotoFileList() {
			var res = await this.$API.file.photo.getMyPhotoFileList.get({photoId: this.currentPhotoId});
			if (res.code == 200) {
				this.photoFileList = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getPhotoRole()
		{
			var res = await this.$API.file.photo.getPhotoRole.get({photoId: this.currentPhotoId});
			if (res.code == 200) {
				this.manageRole = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		previewImage(url){
			this.previewList=[];
			this.previewList.push(url)
		},
		//本地更新数据
		uploadSuccess() {
			this.getMyPhotoFileList()
		},
		async handleCommand(command) {
			if (command == "down") {
				this.$nextTick(() => {
					window.open(sysConfig.API_URL+'/get/file/downPhotoImage?photoId=' + this.currentPhotoId + "&fileName="+encodeURIComponent(this.currentPhotoFile)+"&ddtab=true", "_blank");
				})
			}else if(command=="delete")
			{
				var res = await this.$API.file.photo.deletePhotoFile.post({photoId: this.currentPhotoId,fileName:this.currentPhotoFile});
				if (res.code == 200) {
					this.$message.success("操作成功");
					this.getMyPhotoFileList();
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}
		},
		openMenu(e,item) {
			this.currentPhotoId = item.photoId;
			this.currentPhotoFile = item.fileName;
			this.$refs.fileContextmenu.openMenu(e);
		},
	}
}
</script>

<style>
.nameClass {
	margin: auto;
	-webkit-line-clamp: 3;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	overflow: hidden;
	text-overflow: ellipsis;
	max-height: 55px;
	word-break: break-all;
	}
</style>
